#include "INA226.h"

extern struct bflb_device_s *i2c0;

 //读2个字节,reg_addr为读的寄存器地址
uint16_t INA226_Read2Byte(uint8_t reg_addr)
{
	struct bflb_i2c_msg_s msgs[2];
    uint8_t reg_data[2];
    uint16_t ret_data;
	
    msgs[0].addr = IN226_ADDR;
    msgs[0].flags = I2C_M_NOSTOP;
    msgs[0].buffer = &reg_addr;
    msgs[0].length = 1;

    msgs[1].addr = IN226_ADDR;
    msgs[1].flags = I2C_M_READ;
    msgs[1].buffer = reg_data;
    msgs[1].length = 2;
    bflb_i2c_transfer(i2c0, msgs, 2);
    ret_data = (reg_data[0]<<8) | reg_data[1];
	return ret_data;
}
 
//写2个字节，reg_addr为写的寄存器地址,reg_data为写的数据
int INA226_Write2Byte(uint8_t reg_addr,uint16_t reg_data)
{        	
	struct bflb_i2c_msg_s msgs[2];
  uint8_t buffer[2];
  buffer[0] = (uint8_t)((reg_data&0xFF00)>>8);
  buffer[1] = (uint8_t)reg_data&0x00FF;

    msgs[0].addr = IN226_ADDR;
    msgs[0].flags = I2C_M_NOSTOP;
    msgs[0].buffer = &reg_addr;
    msgs[0].length = 1;
    
    msgs[1].addr = IN226_ADDR;
    msgs[1].flags = 0;
    msgs[1].buffer = buffer;
    msgs[1].length = 2;

    return bflb_i2c_transfer(i2c0, msgs, 2);
}
void INA226_Readid(void)
{
  printf("INA226 man id is %x,\n",INA226_Read2Byte(Man_ID_Reg));
  printf("INA226 id is %x,\n",INA226_Read2Byte(ID_Reg));
} 

void INA226_Init(void)
{
  //写配置寄存器
  INA226_Write2Byte(Config_Reg, 0x4527);//0100_010_100_100_111 //16次平均,1.1ms,1.1ms,连续测量分流电压和总线电压
  //写校准寄存器
  INA226_Write2Byte(Calib_Reg, 0x0A00);//LSB选择0.02mA,分压电阻选0.0001R Cal=0.00512/(0.02mA*0.0001R)=2560
  printf("INA226 Inited\n");
  INA226_Readid();
}

void INA226_TEST(void)
{
  printf("====================================\r\n");
  printf("data=%-5d, Bus_V   =%f V\r\n", INA226_Read2Byte(Bus_V_Reg),INA226_Read2Byte(Bus_V_Reg)*1.25*0.001);
  printf("data=%-5d, Shunt_V =%f mV\r\n", INA226_Read2Byte(Shunt_V_Reg),INA226_Read2Byte(Shunt_V_Reg)*2.5*0.001);
  printf("data=%-5d, Curent  =%f mA\r\n", INA226_Read2Byte(Current_Reg),INA226_Read2Byte(Current_Reg)*0.02);
  printf("data=%-5d, Power   =%f mW\r\n", INA226_Read2Byte(Power_Reg),INA226_Read2Byte(Power_Reg)*0.02*25);
  printf("====================================\r\n");
}
 
//获取总线电压mV
float INA226_GetVoltage(void)
{
  float Bus_V;
  Bus_V = INA226_Read2Byte(Shunt_V_Reg)*1.25*0.001;//总线电压LSB固定1.25mV
 // printf("data=%-5d, Bus_V  =%f mV\r\n", INA226_Read2Byte(Current_Reg),Bus_V);
  return Bus_V;
}
 
//分流电压mV
float INA226_GetShuntVoltage(void)
{
  float Shunt_V;
  Shunt_V=INA226_Read2Byte(Shunt_V_Reg)*2.5*0.001;//分流电压LSB固定2.5uV
 // printf("data=%-5d, Shunt_V=%f mV\r\n", INA226_Read2Byte(Current_Reg),Shunt_V);
  return Shunt_V;
}
 
//获取分流电流mA
float INA226_GetShunt_Current(void)
{
   float Curent;
   Curent=INA226_Read2Byte(Current_Reg)*0.02; //分流电流LSB选择0.02mA
   //printf("data=%-5d, Curent =%f mA\r\n", INA226_Read2Byte(Current_Reg),Curent);
   return Curent;
}
 
//获取功率
float INA226_Get_Power(void)
{
  float Power;
  Power=INA226_Read2Byte(Power_Reg)*0.02*25;//功率LSB固定分流电流LSB的25倍
  //printf("data=%-5d, Power=%f mW\r\n", INA226_Read2Byte(Power_Reg),Power);
  return Power;
}
 
 
 
 
